// pages/directDetail/directDetail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    current: 0,
    activeColor: '#b3b3b3',
    indicatorColor: '#f2f1f1',
    circular: "true"
  },
  swichNav: function (e) {//点击切换样式
    var that = this;
    if (this.data.current === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        current: e.target.dataset.current
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   // console.log(options);

    getBackDataHot(this, options.id)
    this.setData({
      imageUrl: app.globalData.imageUrl,
      //directId: options.directId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('cartLists')) {//先判断购物车有无商品
      this.setData({
        cartList: wx.getStorageSync('cartLists')
      })
    } else {
      this.setData({
        cartList: []
      })
    }
  },
    selectInfoDirect:function(){
    var that=this;
    wx.navigateTo({
      url: '../directInfo/directInfo?directId=' + that.data.unitProduct.directId,
    })
  }
  ,
  addToCart: function (e) {//加入购物车
    var that = this;
    if (that.data.cartList.length > 0) {
      var config = false;
      for (var x in that.data.cartList) {
        if (that.data.cartList[x].productId == e.target.dataset.id) {
          if (that.data.cartList[x].quantity == that.data.cartList[x].repertory) {//如果增加的数量大于库存，则取消增加；
            wx.showToast({
              title: "库存不足",
              icon: "loading"
            })
          } else {
            that.data.cartList[x].quantity += 1;
          }
          config = true;
        }
      }
      if (!config) {
        getCartInfo(that, e)//加入购物车
      }
    } else {
      getCartInfo(that, e)//加入购物车
    }
    wx.setStorageSync('cartLists', this.data.cartList)
   // console.log(wx.getStorageSync('cartLists'))
  },
  buy: function (e) {//立即购买
    var that = this;
    if (that.data.cartList.length > 0) {
      var config = false;
      for (var x in that.data.cartList) {
        if (that.data.cartList[x].productId == e.target.dataset.id) {
          if (that.data.cartList[x].quantity == that.data.cartList[x].repertory) {//如果增加的数量大于库存，则取消增加；
            wx.showToast({
              title: "库存不足",
              icon: "loading"
            })
          } else {
            that.data.cartList[x].quantity += 1;
          }
          config = true;
        }
      }
      
      if (!config) {
        getCartInfo(that, e)//加入购物车
      }
    } else {
      getCartInfo(that, e)//加入购物车
    }
    wx.setStorageSync('cartLists', this.data.cartList)
   // console.log(wx.getStorageSync('cartLists'))
    wx.switchTab({
      url: '../directBuy/directBuy',
      
    })
  }
})

function getBackDataHot(that, _id) {//获取单个商品的详情
  wx.request({
    url: app.globalData.allUrl + "/shop/goodsinfo",
    data: {
      id: _id
    },
    header: { 'content-type': "application/x-www-form-urlencoded" },
    method: 'POST',
    success: function (res) {
   //   console.log(res.data)
      if (res.data) {
        var imgUrls = [];
        that.setData({
          imgUrls: res.data.banner,
          unitProduct: res.data.data,
           imgDetail:res.data.img
        })
      }
    }
  })
}


function getCartInfo(that, e) {//加入购物车，将商品信息储存在当前页面
  var CartName = e.target.dataset.name;
  var CartPrice = e.target.dataset.price;
  var CartID = e.target.dataset.id;
  var shopsId = e.target.dataset.directid;
  var Cartquantity = 1;
  var totalPrices = "";
  var Cartimg = e.target.dataset.img;
 // var Cartdirectid = e.target.dataset.directid;
  var Cartrepertory = e.target.dataset.repertory
  var carts = { productName: CartName, unitPrice: CartPrice, productId: CartID, quantity: Cartquantity, totalPrices: totalPrices, repertory: Cartrepertory, img: Cartimg, directId: 1, shopsId: shopsId}
  that.data.cartList.push(carts);
}